package Test;

import static org.junit.Assert.*;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;

import junit.framework.Assert;

import org.junit.After;
import org.junit.AfterClass;
import org.junit.Before;
import org.junit.BeforeClass;
import org.junit.Test;

import ControlLayer.PersonCtr;
import ControlLayer.RoomBookingCtr;
import ControlLayer.RoomBookingException;
import ControlLayer.SaleCtr;
import DBLayer.DBRoomBooking;
import DBLayer.DBSale;
import ModelLayer.KeyWord;
import ModelLayer.RoomBooking;

public class RoomBookingCtrTest {
	private SaleCtr saleCtr;
	private PersonCtr personCtr;
	private RoomBookingCtr rbctr;
	private Date date1;
	private Date date2;
	private Date date3;

	@BeforeClass
	public static void setUpBeforeClass() throws Exception {
	}

	@AfterClass
	public static void tearDownAfterClass() throws Exception {
	}

	@Before
	public void setUp() throws Exception {
		saleCtr = new SaleCtr();
		personCtr = new PersonCtr();
		rbctr = new RoomBookingCtr();
		date1 = new SimpleDateFormat("dd/MM/yyyy").parse("01/07/2013");
		date2 = new SimpleDateFormat("dd/MM/yyyy").parse("02/07/2013");
		date3 = new SimpleDateFormat("dd/MM/yyyy").parse("03/07/2013");
	}

	@After
	public void tearDown() throws Exception {
	}

	@Test
	public void testInsertAndGetRoomBooking() {
		int guestId = personCtr.createGuest("Sam", "Smith", "Oktobervej 1",
				"Denmark", "43567890", "asjd@hotmail.com", "Guest", "A12366",
				0, 0);
		int employeeId = personCtr.createEmployee("Fay", "Stay", "SunnyRoad 4",
				"Morroco", "4563456", "stjy@gmail.com", "Employee",
				"23565589535", "Receptionist");
		int saleId = 0;
		try {
			saleId = saleCtr.insertSale2(guestId, employeeId, 100.0,
					"SaleUnpaid", "31/01/2013", "01/02/2013", "CreditCard");
		} catch (ParseException e) {
			e.printStackTrace();
		}
		rbctr.insertRoomBooking(saleId, date1, date1, date2, 50.0, 100.0,
				"DepositUnpaid");
		RoomBooking rb = rbctr.getRoomBooking(saleId);
		Assert.assertEquals(saleId, rb.getId());
		Assert.assertEquals(100.0, rb.getRent());
		Assert.assertEquals(50.0, rb.getDeposit());
		Assert.assertEquals("DepositUnpaid", rb.getPayStatus().toString());
		Assert.assertEquals("01/07/2013",
				new SimpleDateFormat("dd/MM/yyyy").format(rb.getCreateDate()));
		Assert.assertEquals("01/07/2013",
				new SimpleDateFormat("dd/MM/yyyy").format(rb.getStartDate()));
		Assert.assertEquals("02/07/2013",
				new SimpleDateFormat("dd/MM/yyyy").format(rb.getEndDate()));
		DBRoomBooking dbRB = new DBRoomBooking();
		dbRB.deleteRoomBooking(saleId);
		DBSale dbsale = new DBSale();
		dbsale.deleteSale(saleId);
		personCtr.deleteGuest(guestId);
		personCtr.deleteEmployee(employeeId);
	}

	// @Test
	// public void testUpdateDBRoomBookingRoomBooking() {
	// int guestId = personCtr.createGuest("Sam", "Smith", "Oktobervej 1",
	// "Denmark", "43567890", "asjd@hotmail.com", "Guest", "A12366",
	// 0, 0);
	// int employeeId = personCtr.createEmployee("Fay", "Stay", "SunnyRoad 4",
	// "Morroco", "4563456", "stjy@gmail.com", "Employee",
	// "23565589535", "Receptionist");
	// int saleId = 0;
	// try {
	// saleId = saleCtr.insertSale2(guestId, employeeId, 100.0,
	// "SaleUnpaid", "31/01/2013", "01/02/2013", "CreditCard");
	// } catch (ParseException e) {
	// e.printStackTrace();
	// }
	// rbctr.insertRoomBooking(saleId, date1, date1, date2, 50.0, 100.0,
	// "DepositUnpaid");
	// RoomBooking newrb = new RoomBooking();
	// newrb.setId(saleId);
	// newrb.setCreateDate(date2);
	// newrb.setStartDate(date2);
	// newrb.setEndDate(date3);
	// newrb.setDeposit(100.0);
	// newrb.setRent(200.0);
	// newrb.setPayStatus(KeyWord.DepositPaid);
	// try {
	// //TODO Auto-generated catch block
	// rbctr.updateRoomBooking(saleId, newrb);
	// } catch (RoomBookingException e) {
	//
	// e.printStackTrace();
	// }
	// RoomBooking rb = rbctr.getRoomBooking(saleId);
	// Assert.assertEquals(saleId, rb.getId());
	// Assert.assertEquals(200.0, rb.getRent());
	// Assert.assertEquals(100.0, rb.getDeposit());
	// Assert.assertEquals("DepositPaid", rb.getPayStatus().toString());
	// Assert.assertEquals("02/07/2013",
	// new SimpleDateFormat("dd/MM/yyyy").format(rb.getCreateDate()));
	// Assert.assertEquals("02/07/2013",
	// new SimpleDateFormat("dd/MM/yyyy").format(rb.getStartDate()));
	// Assert.assertEquals("03/07/2013",
	// new SimpleDateFormat("dd/MM/yyyy").format(rb.getEndDate()));
	// DBRoomBooking dbRB = new DBRoomBooking();
	// dbRB.deleteRoomBooking(saleId);
	// DBSale dbsale = new DBSale();
	// dbsale.deleteSale(saleId);
	// personCtr.deleteGuest(guestId);
	// personCtr.deleteEmployee(employeeId);
	// }

	@Test
	public void testDeleteRoomBooking() {
		int guestId = personCtr.createGuest("Sam", "Smith", "Oktobervej 1",
				"Denmark", "43567890", "asjd@hotmail.com", "Guest", "A12366",
				0, 0);
		int employeeId = personCtr.createEmployee("Fay", "Stay", "SunnyRoad 4",
				"Morroco", "4563456", "stjy@gmail.com", "Employee",
				"23565589535", "Receptionist");
		int saleId = 0;
		try {
			saleId = saleCtr.insertSale2(guestId, employeeId, 100.0,
					"SaleUnpaid", "31/01/2013", "01/02/2013", "CreditCard");
		} catch (ParseException e) {
			e.printStackTrace();
		}
		rbctr.insertRoomBooking(saleId, date1, date1, date2, 50.0, 100.0,
				"DepositUnpaid");
		Assert.assertEquals(1, rbctr.deleteRoomBooking(saleId));

		DBSale dbsale = new DBSale();
		dbsale.deleteSale(saleId);
		personCtr.deleteGuest(guestId);
		personCtr.deleteEmployee(employeeId);
	}

}
